<?php

namespace Jinxing\Admin;

use Illuminate\Support\Str;
use Illuminate\Support\Facades\Event;
use Jinxing\Admin\Commands\ViewCommand;
use Illuminate\Support\ServiceProvider;
use Jinxing\Admin\Http\Middleware\Auth;
use Jinxing\Admin\Http\Middleware\Json;
use Jinxing\Admin\Composers\MenusComposer;
use Jinxing\Admin\Commands\GenerateCommand;
use Jinxing\Admin\Commands\AssetLinkCommand;
use Jinxing\Admin\Commands\ControllerCommand;
use Jinxing\Admin\Composers\DataTableComposer;
use Jinxing\Admin\Composers\BreadCrumbsComposer;
use Illuminate\Database\Events\StatementPrepared;

class AdminProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        // 修改查询信息
        if (config('database.fetch')) {
            Event::listen(StatementPrepared::class, function ($event) {
                $event->statement->setFetchMode(config('database.fetch'));
            });
        }

        // 注册组件信息
        view()->composer([
            'admin::common.breadcrumbs',
            'admin::common.top_nav',
            'admin::common.menu',
        ], BreadCrumbsComposer::class);
        view()->composer(['admin::common.datatable'], DataTableComposer::class);
        view()->composer(['admin::common.menu', 'admin::common.top_nav'], MenusComposer::class);

        // 注册命令行工具
        if ($this->app->runningInConsole()) {
            $this->commands([
                AssetLinkCommand::class,
                GenerateCommand::class,
                ControllerCommand::class,
                ViewCommand::class,
            ]);
        }

        // 加载视图信息
        $this->loadViewsFrom(admin_path('resources/views'), 'admin');
        $this->loadMigrationsFrom(admin_path('database/migrations'));
        $this->publishes([
            admin_path('../config/admin.php')            => config_path('admin.php'),
            admin_path('resources/lang/zh-CN/admin.php') => resource_path('lang/zh-CN/admin.php'),
            admin_path('resources/lang/zh-CN/error.php') => resource_path('lang/zh-CN/error.php'),
        ], 'config');

        // 注册路由配置
        $routeConfig = [
            'namespace'  => 'Jinxing\Admin\Http\Controllers',
            'prefix'     => $this->app['config']->get('admin.route.prefix'),
            'domain'     => $this->app['config']->get('admin.route.domain'),
            'middleware' => [
                'web',
                Auth::class,
            ],
        ];

        // 注册路由
        $this->app['router']->group($routeConfig, function ($router) {
            $uri = request()->path();
            // 前缀匹配
            if (Str::startsWith($uri, $this->app['config']->get('admin.route.prefix'))) {
                $uri    = trim(Str::replaceFirst($this->app['config']->get('admin.route.prefix'), '', $uri), '/');
                $params = explode('/', $uri);
                array_studly_case($params);
                // 获取到 控制器 和 action
                list($controller, $action) = get_controller_action($params);
                $router->match(['get', 'post'], $uri, $controller . '@' . $action);
            }
        });
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        $this->mergeConfigFrom(admin_path('../config/admin.php'), 'admin');
    }
}
